package com.neu.ui.dashboard;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.neu.R;
import com.neu.adapter.NewsAdapter;
import com.neu.bean.NewsBean;
import com.neu.bean.NewsCategory;
import com.neu.bean.Rotation;
import com.neu.ui.BaseFragment;
import com.neu.ui.home.HomeViewModel;
import com.neu.util.GlideImageLoader;
import com.neu.util.VolleyUtil;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class DashboardFragment extends BaseFragment {
    private Banner newBanner;
    private HomeViewModel homeViewModel;
    private DashboardViewModel dashboardViewModel;
    private TabLayout tabNews;
    private RecyclerView recNews;


    @Override
    protected int layoutId() {
        return R.layout.fragment_dashboard;
    }

    @Override
    protected void initView(View view) {
        newBanner=view.findViewById(R.id.newsBanner);
        tabNews=view.findViewById(R.id.tabNews);
        recNews=view.findViewById(R.id.recNews);
        recNews.setLayoutManager(new LinearLayoutManager(getContext()));

        //点击事件
        tabNews.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.d("TAG", "onTabSelected: "+tab.getText()+" "+tab.getTag());
                dashboardViewModel.loadNews(tab.getTag().toString());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    @Override
    protected void initData() {
        homeViewModel=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(HomeViewModel.class);
        dashboardViewModel=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(DashboardViewModel.class);
       //添加轮播
        initBanner();
        //添加新闻分类
        initNewsCategory();
        //获得新闻
        initNews();
    }

    private void initNews() {
        dashboardViewModel.getNews().observe(getViewLifecycleOwner(), new Observer<NewsBean>() {
            @Override
            public void onChanged(NewsBean newsBean) {
                List<NewsBean.RowsBean> list=newsBean.getRows();
                recNews.setAdapter(new NewsAdapter(list));
            }
        });
    }

    private void initNewsCategory() {
        dashboardViewModel.getNewsCategory().observe(getViewLifecycleOwner(), new Observer<NewsCategory>() {
            @Override
            public void onChanged(NewsCategory newsCategory) {
                List<NewsCategory.DataBean> list=newsCategory.getData();
                for(NewsCategory.DataBean d:list){
                    int id=d.getId(); //分类id
                    String name=d.getName();//类别名称
                    //新建
                    TabLayout.Tab tab=tabNews.newTab();
                    tab.setText(name);//显示出来的名称
                    tab.setTag(id);//不显示出来
                    tabNews.addTab(tab);
                }
            }
        });
    }

    private void initBanner() {
        homeViewModel.getRotation().observe(getViewLifecycleOwner(), new Observer<Rotation>() {
            @Override
            public void onChanged(Rotation rotation) {
                List<Rotation.RowsBean> list=rotation.getRows();
                //存放图片地址
                List<String> imgUrlList=new ArrayList<>();
                for(Rotation.RowsBean r:list){
                    String url= VolleyUtil.BASE_URL+r.getAdvImg();//图片地址
                    imgUrlList.add(url);
                }
                newBanner.isAutoPlay(true);
                newBanner.setImages(imgUrlList);
                newBanner.setImageLoader(new GlideImageLoader());
                newBanner.start();
            }
        });
    }
}